// pages/order/bill/bill.js
const handler = require("../../../utils/function.js");
var app = getApp(),
    api = require("../../../api.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
      img: handler.driverImg,
        driver: {},
        order: {},
        is_cash:'',
        feeDetail: {
            speedFee: 0,
            roadToll: 2,
            otherFee: 3,
            order_no: '',
        }
    },
    skipPageHandler (e) {
      var order_no = e.currentTarget.dataset.order_no;
      wx.navigateTo({
        url: '/pages/userOrder/orderTrack/orderTrack?order_no=' + order_no,
      })
    },
    intoPageHandler: function(e) {
        console.log('发起收款参数', e)
        var that = this;
        console.log('抢单结果22', that.data.order_no)
        var formid = e.detail.formId;
        let order = that.data.order;
        app.request({
            url: api.order.initiate,
            method: 'post',
            data: {
                order_no: e.currentTarget.dataset.order_no,
                actual_price: e.currentTarget.dataset.paid,
                formid: formid
               /* speedfee: e.currentTarget.dataset.speedFee,
                tolls: e.currentTarget.dataset.roadToll,
                other: e.currentTarget.dataset.otherFee,*/
            },
            success: function(rs) {
                if (rs.code == 1) {
                  if (order.uid != order.driverid) {
                    app.gateway(e.currentTarget.dataset.order_no,3)
                  }
                  wx.redirectTo({
                    url: e.currentTarget.dataset.skip + '?paid=' + e.currentTarget.dataset.paid + '&speedFee=' + e.currentTarget.dataset.speedfee + '&roadToll=' + e.currentTarget.dataset.roadtoll + '&otherFee=' + e.currentTarget.dataset.otherfee + '&order_no=' + this.data.order_no, })
                    // handler.intoPageHandler()
                } else {
                    wx.showToast({
                        title: "你已催款，请稍后",
                        duration: 1000,
                        icon:'none'
                    })
                }
            }
        })
    },
    callPhoneHandler: function(e) {
      console.log(e.currentTarget.dataset.phone)
      handler.callPhoneHandler(e.currentTarget.dataset.phone);
    },
    // addHandler 加 事件
    addAdnReduceHandler: function(e) {
        const types = e.currentTarget.dataset.types;
        const params = e.currentTarget.dataset.params;
        const feeDetail = this.data.feeDetail;
        if (types === "add") {
            feeDetail[params]++;
            this.setData({
                feeDetail
            });
        } else {
            if (feeDetail[params] > 0) {
                feeDetail[params]--;
                this.setData({
                    feeDetail
                });
            }
        }
    },
    //获取现金
    getCash:function(e){
      let that=this;
      let order = this.data.order;
      app.request({
        url: api.order.cash,
        method: 'post',
        data: {
          order_no: that.data.order_no,
          // price: e.currentTarget.dataset.paid,
          price: order.actual_price,
        },
        success: function (rs) {
          if(rs.code==1){
            if(order.driverid != order.uid) {
              app.gateway(that.data.order_no, 4)
            }
            wx.navigateBack({
              delta:1
            })
          }
          wx.showToast({
            title: rs.msg,
            icon:'none'
          })
         }
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      let that = this;
      that.setData({
          order_no: options.order_no,
          // longitude: options.longitude,
          // latitude: options.latitude,
      })
      // 获取优惠券
      if (options.uid) {
        app.shareCouponsMessage(options.uid)
      }
      app.request({
        url: api.order.is_cash,
        method: 'post',
        data: {
          // _uniacid: that.data.order_no,
          // price: e.currentTarget.dataset.paid,
        },
      
        success:function(res){
          console.log(res.data)
          that.setData({
            is_cash:res.data.is_cash
          })
        }
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        console.log('结束行程结果', that.data.order_no);
        wx.showLoading({})
        app.request({
            url: api.order.getfind,
            method: 'post',
            data: {
                order_no: that.data.order_no,
                // longitude: that.data.longitude,
                // latitude: that.data.latitude
            },
            success: function(rs) {
              // 自动收款
              that.getCash()
              wx.hideLoading()
                if (rs.code == 1) {
                    that.setData({
                        order: rs.data
                    })
                    wx.getStorageSync("server_phone", rs.data.passenger_phone)
                } else {
                    wx.showToast({
                        title: rs.msg,
                    })
                    wx.redirectTo({
                      url: '../orderGoing/orderGoing?order_no=' + that.data.order_no　 // 页面 A
                    })
                }
            }
        })
    },
    // 获取formId
  formSubmit: function (e) {
    var user_id = wx.getStorageSync('user_info').id;
    var formid = e.detail.formId;
    // console.log(formid);
    if (user_id && formid) {
      var para = {
        form_id: formid,
        user_id: user_id
      };
      var that = this;
      var isTrue = e.currentTarget.dataset.istrue;
      if (isTrue == "true") {
        var user_type = wx.getStorageSync('user_info').user_type;
        if (user_type == 1) {
          this.login_test('../teacher-appointment-record/index');
        } else {
          this.login_test('../user-appointment-record/index');
        }

      } else {
        handler.requestData("storageFormid", para, function (res) { console.log(res) });
        that.handler_next_page(e)
      }


    }
  },
  // 用户分享
  onShareAppMessage: function () {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;
    console.log(app.shareAppMessage())
    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
})